package com.example.huayang.appdome.base;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.widget.Toast;

import com.example.huayang.appdome.utils.LoadingDialog;

import java.util.Stack;

/**
 * Created by WANG on 2017/9/20.
 * activity 基类
 */

public class BaseActivity extends AppCompatActivity {

    private LoadingDialog loadingDialog;//加载动画对话框
    private Stack<BaseFragment> fragmentStack;//将fragment存进栈对列
    private FragmentManager fragmentManager;//fragment管理器
    private int paramInt;//用于存放fragment的布局id
    public Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this); // 添加Activity到堆栈
        intent = new Intent();
    }

    @Override
    protected void onResume() {
        /**
         * 设置为竖屏
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (outState != null) {//解决fragment有时候context为空的情况
            outState.remove("android:support:fragments");
        }
    }

    /**
     * 设置装fragment的layout
     *
     * @param paramInt 存放fragment的布局id
     */
    public void setParamInt(int paramInt) {
        this.paramInt = paramInt;
    }

    /**
     * 启动fragment 主要是应对tab效果
     *
     * @param b 需要传给fragment的内容
     */
    public void startFragment(Bundle b, BaseFragment f) {
        if (f == null)
            return;
        if (fragmentManager == null) {
            fragmentManager = getFragmentManager();
        }
        if (fragmentStack == null) {
            fragmentStack = new Stack<BaseFragment>();
        }
        try {
            BaseFragment currentF = null;
            if (fragmentStack.size() > 0) {// 当前堆栈容量大于0，隐藏栈顶的对象
                FragmentTransaction t1 = fragmentManager.beginTransaction();//fragment事务
                currentF = fragmentStack.peek();
                t1.hide(currentF).commitAllowingStateLoss();
            }
            FragmentTransaction t = fragmentManager.beginTransaction();//fragment事务
            // 针对单例模式的fragment做处理
            if (fragmentStack.contains(f) && f.registStartMode() == BaseFragment.singleTask) {
                fragmentStack.remove(f);
                fragmentStack.push(f);
                f.setBundle(b);
                t.show(f).commitAllowingStateLoss();
                return;
            }
            f.setBundle(b);
            t.add(paramInt, f).commitAllowingStateLoss();
            fragmentStack.push(f);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param content 展示对话框的内容
     */
    public void showDialog(String content) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);//初始化对化框
        }
        loadingDialog.showLoading(content);
    }

    /**
     * 取消对话框
     */
    public void dismiss() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);//初始化对化框
        }
        loadingDialog.dismiss();
    }

    /**
     * 按设定的时间长短提示
     *
     * @param content  提示内容
     * @param duration 设定的时间 单位毫秒
     */
    public void showTips(String content, int duration) {
        Toast toast = Toast.makeText(this, content, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);// 结束Activity&从堆栈中移除
        if (fragmentStack != null) {//销毁fragment单列对象，和资源
            fragmentStack.clear();
        }
    }

    /**
     * 界面跳转且关闭此界面
     *
     * @param cla 要开车去的地方
     */
    public void startAtvAndFinish(Class cla) {
        intent.setClass(this, cla);
        startActivity(intent);
        finish();
    }

    /**
     * 界面跳转不关闭此界面
     *
     * @param cla 要开车去的地方
     */
    public void startAtvDonFinish(Class cla) {
        intent.setClass(this, cla);
        startActivity(intent);
    }

    /**
     * 带数据返回的界面跳转
     *
     * @param cla  类
     * @param requestCode 跳转码
     * @return null
     * @author lc
     * @time 2016/9/28 18:10
     */
    public void startAtvDonFinishForResult(Class cla, int requestCode) {
        intent.setClass(this, cla);
        startActivityForResult(intent, requestCode);
    }

}